<!DOCTYPE html>
<html lang="en">

<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="./js/vue2.js"></script>
    <link rel="stylesheet" href="./css/animate.css">

    <link rel="stylesheet" href="./css/弹框 .css">
    <style>
        #app {
            width: 600px;
            margin: 10px auto;
        }

        .tb {
            border-collapse: collapse;
            width: 100%;
        }

        .tb th {
            background-color: #0094ff;
            color: white;
        }

        .tb td,
        .tb th {
            padding: 5px;
            border: 1px solid black;
            text-align: center;
        }

        .add {
            padding: 5px;
            border: 1px solid black;
            margin-bottom: 10px;
        }
    </style>
</head>

<body>
    <body>
        <div id="app">
            <div class="add">
                编号:
                <input type="text" v-model="product.id" v-myfocus v-color='"red"'> 品牌名称:
                <input type="text" v-model="product.name" @keydown.enter="tianjia" v-color='"blue"'>
                <input type="button" value="添加" @click="tianjia">
            </div>
            <div class="add">
                品牌名称:
                <input type="text" placeholder="请输入搜索条件" v-model="searchValue">
            </div>
            <div>
                <table class="tb">
                    <tbody>
                        <tr>
                            <th>编号</th>
                            <th>品牌名称</th>
                            <th>创立时间</th>
                            <th>操作</th>
                        </tr>
                        <tr v-for='(value,index) in listData' :key="index">
                            <td>{{value.id}}</td>
                            <td>{{value.name}}</td>
                            <td>{{value.ctime | customdata('  ')}}</td>
                            <td>
                                <a href="#" @click.prevent="deleteData(index)">删除</a>
                                <!--该index是 上面for 循环里面每一个的id 
                                而点击事件的id也就是这个id 所以不需要再进行判断了-->
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4" v-show='listData.length==0'>此处没有更多的数据了</td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <!-- 弹框 按钮 -->
            <transition name="" mode="" enter-active-class="animated zoomIn" ener-active-class="animated zoomOut">
                <div id="popBox" v-show="isshow">
                    <div class="close">
                        <a href="javascript:void(0)" @click="close">关闭</a>
                    </div>
                    <input type="button" value="确定" id="anniu1" @click="sure">
                    <input type="button" value="取消" id="anniu2" @click="cancel">
                </div>
            </transition>
        </div>
    </body>
</body>
<script>
    var list = [
        { id: 1, name: 'LOREAL欧莱雅', ctime: '2019-06-22T04:43:51.049Z' },
        { id: 2, name: 'Dreamtimes', ctime: '2019-06-22T04:43:51.049Z' },
        { id: 3, name: 'Skin food思亲肤', ctime: '2019-06-22T04:43:51.049Z' },
        { id: 4, name: '御泥坊', ctime: '2019-06-22T04:43:51.049Z' },
        { id: 5, name: 'CLINIQUE倩碧', ctime: '2019-06-22T04:43:51.049Z' },
        { id: 6, name: '娇韵诗CLARINS', ctime: '2019-06-22T04:43:51.049Z' }
    ];
    // 自定义聚焦指令 
    Vue.directive('myfocus', {
        inserted(el) {
            el.focus();
        }
    })
    // 自定义输入框颜色指令
    Vue.directive('color', {
        inserted(el, binding) {
            el.style.color = binding.value;
        }
    })
    // 自定义时间格式过滤器
    Vue.filter('customdata', function (time, sc) {
        var date = new Date(time);
        var year = date.getFullYear();
        /* 在日期格式中，月份是从0开始的，因此要加0
         * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
         * */
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        // 拼接
        return year + sc + month + sc + day + " " + hours + ":" + minutes + ":" + seconds;
    })
    // 创建对象
    var ms = new Vue({
        el: "#app",
        data: {
            product: { id: '', name: '', ctime: new Date() },
            list: list,
            colorvalue: 'red',
            isshow: false,
            deleteRow: '',
            searchValue: ''
        },
        methods: {
            tianjia() {
                this.product.ctime = new Date();
                // 添加数据到品牌列表中
                this.list.push({ ...this.product });
                // 清空product 
                this.product = { id: '', name: '', ctime: Date() };
            },
            deleteData(id) {
                // console.log(this)
                // var index = this.list.findIndex((item) => {
                //     return item.id === id;
                // })
                // console.log(id)
                // console.log(this.deleteRow)

                this.isshow = true;
                this.deleteRow = id;
                // console.log(this.deleteRow)
            },
            sure() {
                // console.log(this.deleteRow)
                this.list.splice(this.deleteRow, 1);
                this.isshow = false;
            },
            cancel() {
                this.isshow = false;
            },
            close() {
                this.isshow = false;
            }
        },
        computed: {
            listData() {
                return this.list.filter(e => e.name.toLowerCase().indexOf(this.searchValue.toLowerCase()) != -1)
            }
        }

    })
</script>

</html>